package com.wnxy.admin.controller;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.wnxy.admin.common.ResponseResult;

import com.wnxy.admin.common.handler.Asserts;
import com.wnxy.admin.common.handler.BusinessEnum;
import com.wnxy.admin.common.handler.BusinessException;
import com.wnxy.admin.common.util.TokenUtil;
import com.wnxy.admin.dto.StaffDto;
import com.wnxy.admin.dto.StaffMenuVO;
import com.wnxy.admin.dto.TokenDto;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.wnxy.admin.entity.Staff;
import com.wnxy.admin.service.IStaffService;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.spi.StateFactory;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@CrossOrigin
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Value("${jwt.signature}")
    private String signature;

    @Value("${jwt.accessTimeout}")
    private Integer accessTimeout;

    @Value("${jwt.refreshTimeout}")
    private Integer refreshTimeout;
    private final IStaffService staffServiceImpl;
    private final RedisTemplate redisTemplate;

    public StaffController(IStaffService staffServiceImpl, RedisTemplate redisTemplate) {
        this.staffServiceImpl = staffServiceImpl;
        this.redisTemplate = redisTemplate;
    }

    @PostMapping("/login")
    public ResponseResult login(@RequestBody StaffDto staffDto) {
        Staff staff = staffServiceImpl.login(staffDto);
        //管理员是存在的，生成大小令牌
        Map<String, Object> payload = new HashMap<>();
        payload.put("staffId", staff.getId() + "");
        //IP地址，自己装；2个令牌就产生
        payload.put("exp", LocalDateTime.now().plusMinutes(accessTimeout)
                .format(DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:ss")));
        String accessToken = TokenUtil.createToken(payload, signature);

        payload.put("exp", LocalDateTime.now().plusDays(refreshTimeout)
                .format(DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:ss")));
        String refreshToken = TokenUtil.createToken(payload, signature);

        System.out.printf("accessToken = " + accessToken);
        System.out.printf("refreshToken = " + refreshToken);

        return ResponseResult.ok(new TokenDto(accessToken, refreshToken, staff.getAccount(), staff.getId()));
    }

    //分页，模糊查询员工账号，编号，姓名
    @GetMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult page(@PathVariable Integer pageNum,
                               @PathVariable Integer pageSize,
                               @RequestParam(required = false) String keyword) {
        PageInfo<Staff> page = staffServiceImpl.getAll(pageNum, pageSize, keyword);
        return ResponseResult.ok(page);
    }

    //添加
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Staff staff) {
        staffServiceImpl.add(staff);
        return ResponseResult.ok();
    }

    //禁用选项
    @PostMapping("/disable/{id}")
    public ResponseResult disable(@PathVariable("id") Integer id) {
        Staff staff = staffServiceImpl.getById(id);
        staff.setStatus("禁用");
        staffServiceImpl.updateById(staff);
        return ResponseResult.ok();
    }

    //编辑
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Staff staff) {
        staffServiceImpl.updateById(staff);
        redisTemplate.opsForSet().add("upload-file-db", staff.getImage());
        return ResponseResult.ok();
    }

    //根据ID获取员工
    @GetMapping("/getById")
    public ResponseResult getById(Integer staffId) {
        Staff staff = staffServiceImpl.getById(staffId);
        return ResponseResult.ok(staff);
    }

    //获取全部
    @GetMapping("/getAll")
    public ResponseResult getAll() {
        List<Staff> list = staffServiceImpl.list();
        return ResponseResult.ok(list);
    }

    //获取全部的id和name
    @GetMapping("/findAll")
    public ResponseResult findAll() {
        List<StaffMenuVO> list = staffServiceImpl.findAll();
        return ResponseResult.ok(list);
    }
    //根据ids查询对应的员工名称
    @GetMapping("/getName/{ids}")
    public ResponseResult getName(@PathVariable("ids") List<Integer> ids){
        if (ids.isEmpty()){
            throw new BusinessException(BusinessEnum.IDS_IS_NULL);
        }
        List<String> names =  staffServiceImpl.getName(ids);
        return ResponseResult.ok(names);
    }

}
